<?php
require_once 'PHPExcel/PHPExcel.php';

/**
 * 유틸리티 - Excel 생성
 *
 * 참고 : PHPExcel 샘플 및 예제코드는  아래 link에서 제공됩니다.
 *
 * PHPExcel
 *
 * Pear Installation :
 *     pear channel-discover pear.pearplex.net
 *     pear install pearplex/PHPExcel
 *
 * Copyright (C) 2006 - 2010 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * <code>
 * $utilExcel = new utilExcel();
 * $utilExcel->getProperties()->setCreator("Maarten Balliauw")
 *       ->setLastModifiedBy("Maarten Balliauw")
 *       ->setTitle("Office 2007 XLSX Test Document")
 *       ->setSubject("Office 2007 XLSX Test Document")
 *       ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
 *       ->setKeywords("office 2007 openxml php")
 *       ->setCategory("Test result file");
 *
 * $utilExcel->setActiveSheetIndex(0);
 * $utilExcel->getActiveSheet()->setCellValue('B1', 'Invoice');
 * $utilExcel->getActiveSheet()->setCellValue('D1', PHPExcel_Shared_Date::PHPToExcel( gmmktime(0,0,0,date('m'),date('d'),date('Y')) ));
 * $utilExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15);
 * $utilExcel->getActiveSheet()->setCellValue('E1', '#12566');
 *
 * $utilExcel->getActiveSheet()->setCellValue('A3', 'Product Id');
 * $utilExcel->getActiveSheet()->setCellValue('B3', 'Description');
 * $utilExcel->getActiveSheet()->setCellValue('C3', 'Price');
 * $utilExcel->getActiveSheet()->setCellValue('D3', 'Amount');
 * $utilExcel->getActiveSheet()->setCellValue('E3', 'Total');
 *
 * $utilExcel->getActiveSheet()->setCellValue('A4', '1001');
 * $utilExcel->getActiveSheet()->setCellValue('B4', 'PHP for dummies');
 * $utilExcel->getActiveSheet()->setCellValue('C4', '20');
 * $utilExcel->getActiveSheet()->setCellValue('D4', '1');
 * $utilExcel->getActiveSheet()->setCellValue('E4', '=C4*D4');
 *
 * $utilExcel->getActiveSheet()->setCellValue('A5', '1012');
 * $utilExcel->getActiveSheet()->setCellValue('B5', 'OpenXML for dummies');
 * $utilExcel->getActiveSheet()->setCellValue('C5', '22');
 * $utilExcel->getActiveSheet()->setCellValue('D5', '2');
 * $utilExcel->getActiveSheet()->setCellValue('E5', '=C5*D5');
 *
 * $utilExcel->getActiveSheet()->setCellValue('E6', '=C6*D6');
 * $utilExcel->getActiveSheet()->setCellValue('E7', '=C7*D7');
 * $utilExcel->getActiveSheet()->setCellValue('E8', '=C8*D8');
 * $utilExcel->getActiveSheet()->setCellValue('E9', '=C9*D9');
 *
 * $utilExcel->getActiveSheet()->setCellValue('D11', 'Total excl.:');
 * $utilExcel->getActiveSheet()->setCellValue('E11', '=SUM(E4:E9)');
 *
 * $utilExcel->getActiveSheet()->setCellValue('D12', 'VAT:');
 * $utilExcel->getActiveSheet()->setCellValue('E12', '=E11*0.21');
 *
 * $utilExcel->getActiveSheet()->setCellValue('D13', 'Total incl.:');
 * $utilExcel->getActiveSheet()->setCellValue('E13', '=E11+E12');
 * $utilExcel->writeFile('/tests/excel2007', 'xlsx');
 * </code>
 *
 * @link http://phpexcel.codeplex.com/
 *
 * @package    util
 *
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    1.7.4, 2010-08-26
 *
 * @author    jylee3@simplexi.com
 */
class utilExcel extends PHPExcel
{
    /**
     * Excel 파일 저장
     *
     * <code>
     * // /tests/excel2007.xlsx 생성
     * $utilExcel = new utilExcel();
     * $utilExcel->writeFile('/tests/excel2007', 'xlsx');
     * </code>
     *
     * @param    String    $sFilePath     파일경로(확장자제외)
     * @param    String    $sWriteType    Excel 파일 종류(xlsx,xls)
     *
     * @return     Boolean    성공시 true, 실패시 false
     */
    public function writeFile($sFilePath, $sWriteType='xlsx')
    {
        $sWriter = '';
        $sWriteTypeLower = strtolower($sWriteType);

        switch ($sWriteTypeLower) {
            case 'xlsx' :
                $sWriter = 'Excel2007';
                break;
            case 'xls' :
                $sWriter = 'Excel5';
                break;
        }

        if (!empty($sWriter)) {
            $oWriter = PHPExcel_IOFactory::createWriter($this, $sWriter);
            $oWriter->save($sFilePath.'.'.$sWriteTypeLower);

            return true;
        } else {
            return false;
        }
    }
}
